//% color="#aa278d" 
namespace SugarBox {
    //% block="Motor [port] Speed [speed]"  blockType="command"
    //% speed.shadow="range" speed.params.min=-255 speed.params.max=255 speed.defl=100
    //% port.shadow="dropdown" port.options="MPort"
    export function motor(parameter: any, block: any) {
        let port = parameter.port.code;  
        let speed = parameter.speed.code;
        Generator.addImport("import sugarbox"); 
        Generator.addCode(`sugarbox.motor(${port},${speed});`);  
    }
  
    //% block="Motor [port] Stop"  blockType="command"
    //% port.shadow="dropdown" port.options="MPort"
    export function stop(parameter: any, block: any) {
        let port = parameter.port.code; 
        Generator.addImport("import sugarbox");         
        Generator.addCode(`sugarbox.stop(${port});`);                  
    }

    //% block="2KG Servo [port] Degree [degree]°" blockType="command"
    //% degree.shadow="range" degree.params.min=0 degree.params.max=360 degree.defl=180
    //% port.shadow="dropdown" port.options="SPort"
    export function servo2kg(parameter: any, block: any) {
        let port = parameter.port.code;  
        let degree = parameter.degree.code;
        Generator.addImport("import sugarbox"); 
        Generator.addCode(`sugarbox.servo(${port},${degree});`); 
    }

    //% block="Servo [port] Pulse width[us] us" blockType="command"
    //% us.shadow="range" us.params.min=500 us.params.max=2500 us.defl=1500
    //% port.shadow="dropdown" port.options="SPort"
    export function servo(parameter: any, block: any) {
        let port = parameter.port.code;  
        let us = parameter.us.code;
        Generator.addImport("import sugarbox");         
        Generator.addCode(`sugarbox.servo(${port},${us});`); 
    }


    //% block="---"
    export function noteSep1() {

    }


    //% block="Encoder Motor Init" blockType="command"
    export function eMotorInit(parameter: any, block: any) {
        Generator.addImport("from mpython import *");
        Generator.addImport("import machine");        
        Generator.addImport("from machine import Pin");
        Generator.addImport("import time");    
        Generator.addImport("import sugarbox");                  
        Generator.addCode(`em1 = sugarbox.EMotor(1);`); 
        Generator.addCode(`em2 = sugarbox.EMotor(2);`); 
    } 

    //% block="Encoder Motor [eport] Rotational Speed [rspeed] r/min" blockType="command"
    //% rspeed.shadow="range" rspeed.params.min=-300 rspeed.params.max=300 rspeed.defl=120
    //% eport.shadow="dropdown" eport.options="EPort"
    export function eMotorRun(parameter: any, block: any) {
        let eport = parameter.eport.code;  
        let rspeed = parameter.rspeed.code;
        Generator.addCode(`${eport}.run(${rspeed});`);
    }

    //% block="Encoder Motor [eport] Stop" blockType="command"
    //% eport.shadow="dropdown" eport.options="EPort"
    export function eMotorStop(parameter: any, block: any) {
        let eport = parameter.eport.code;  
        Generator.addCode(`${eport}.stop();`);
    }

    //% block="Get Encoder Motor [eport] Rotational Speed(r/min)" blockType="reporter"
    //% eport.shadow="dropdown" eport.options="EPort"
    export function eMotorGet_rspeed(parameter: any, block: any): number {
        let eport = parameter.eport.code;  
        Generator.addCode(`${eport}.get_rspeed()`);
    }

    //% block="Encoder Motor [eport] rotate [round] round Rotational Speed[rspeed]r/min " blockType="command"
    //% eport.shadow="dropdown" eport.options="EPort"
    //% round.shadow="number" round.defl=1
    //% rspeed.shadow="range" rspeed.params.min=-300 rspeed.params.max=300 rspeed.defl=120
    export function run_for_rotations(parameter: any, block: any) {
        let eport = parameter.eport.code;  
        let round = parameter.round.code;
        let rspeed = parameter.rspeed.code;        
        Generator.addCode(`${eport}.run_for_rotations(${round},${rspeed},wait=True);`);
    }

    //% block="Encoder Motor [eport] rotate [degree]° Rotational Speed[rspeed]r/min " blockType="command"
    //% eport.shadow="dropdown" eport.options="EPort"
    //% degree.shadow="number" degree.defl=180
    //% rspeed.shadow="range" rspeed.params.min=-300 rspeed.params.max=300 rspeed.defl=120
    export function run_for_degrees(parameter: any, block: any) {
        let eport = parameter.eport.code;  
        let rspeed = parameter.rspeed.code;
        let degree = parameter.degree.code;         
        Generator.addCode(`${eport}.run_for_degrees(${degree},${rspeed},wait=True);`);
    }

    //% block="Encoder Motor [eport] rotate [sec]s Rotational Speed[rspeed]r/min " blockType="command"
    //% eport.shadow="dropdown" eport.options="EPort"
    //% sec.shadow="number" sec.defl=1
    //% rspeed.shadow="range" rspeed.params.min=-300 rspeed.params.max=300 rspeed.defl=120
    export function run_for_seconds(parameter: any, block: any) {
        let eport = parameter.eport.code;  
        let sec = parameter.sec.code;
        let rspeed = parameter.rspeed.code;        
        Generator.addCode(`${eport}.run_for_seconds(${sec},${rspeed},wait=True);`);
    }


    //% block="---"
    export function noteSep2() {

    }

    
    //% block="Dual Encoder Motor Wheel diameter[diameter]cm speed[vspeed]cm/s" blockType="command"
    //% diameter.shadow="number" diameter.defl=5.6
    //% vspeed.shadow="number" vspeed.defl=11.2
    export function dualRun_for_vspeed(parameter: any, block: any) {
        let diameter = parameter.diameter.code;  
        let vspeed = parameter.vspeed.code;
        Generator.addCode(`em1.dualRun_for_vspeed(${diameter}, ${vspeed});`);
        Generator.addCode(`em2.dualRun_for_vspeed(${diameter}, -${vspeed});`);
    }


    //% block="---"
    export function noteSep3() {

    }

 
    //% block="Car Init Wheel: diameter[diameter]cm spacing[width]cm Left[left] Right[right]reverse[inversed]" blockType="command"
    //% diameter.shadow="number" diameter.defl=5.6
    //% width.shadow="number" width.defl=0
    //% left.shadow="dropdown" left.options="LMotorPort"
    //% right.shadow="dropdown" right.options="RMotorPort"
    //% inversed.shadow="dropdown" inversed.options="Inversed"
    export function dualEMotorInit(parameter: any, block: any) {
        let diameter = parameter.diameter.code;  
        let width = parameter.width.code;
        let left = parameter.left.code; 
        let right = parameter.right.code;
        let inversed = parameter.inversed.code; 
        Generator.addImport("import sugarbox"); 		
        Generator.addCode(`sugar_robot = sugarbox.EMotorDual(${diameter}, ${width}, ${left}, ${right}, ${inversed});`);
    }
    
    //% block="Car Go straight[distance]° speed[vspeed]cm/s" blockType="command"
    //% distance.shadow="number" distance.defl=10
    //% vspeed.shadow="number" vspeed.defl=11.2
    export function dualEMotorRun(parameter: any, block: any) {
        let distance = parameter.distance.code;  
        let vspeed = parameter.vspeed.code;
        Generator.addImport("import sugarbox"); 
        Generator.addCode(`sugar_robot.run(${vspeed}, ${distance}, wait=True);`);
    }
    
    //% block="Car Angle speed[aspeed]°/s Trun angle[angle]° Forward speed[vspeed]cm/s" blockType="command"
    //% aspeed.shadow="number" aspeed.defl=360
    //% angle.shadow="number" angle.defl=180
    //% vspeed.shadow="number" vspeed.defl=11.2
    export function dualEMotorDrive(parameter: any, block: any) {
        let aspeed = parameter.aspeed.code;  
        let angle = parameter.angle.code;
        let vspeed = parameter.vspeed.code;
        Generator.addImport("import sugarbox");         
        Generator.addCode(`sugar_robot.drive(${aspeed}, ${angle}, ${vspeed}, wait=True);`);
    }


    //% block="---"
    export function noteSep3() {

    }


    //% block="---"
    export function noteSep4() {

    }
	
	
    //% block="get Battery Voltage"  blockType="reporter"
    export function battery(parameter: any, block: any){
        Generator.addImport("import sugarbox"); 
        Generator.addCode(`sugarbox.voltage()`); 
    } 

}

